<?php
$jml_hari = cal_days_in_month(CAL_GREGORIAN, $bulan, $tahun);
?>

<style>
    .angka{
        width: 40px;
    }

    .price{
        width: 200px;
        text-align: right;
        float: right;
    }
</style>

<h1 class="title-popup">Data Gaji</h1>

<div style="padding:10px">
    <div id="content">
        <strong>Nama : </strong><?=$nama?><br>
        <?php if($tanggal_keluar != "" && $tanggal_keluar != "0000-00-00") {?>
        <strong>Lama Kerja : </strong><?=selisih_tanggal($tanggal_masuk, $tanggal_keluar)." (Masuk : $tanggal_masuk, Keluar : $tanggal_keluar)"?><br>
            <?php } else {
            ?>
        <strong>Lama Kerja : </strong><?=selisih_tanggal($tanggal_masuk, date("Y-m-d"))." (Masuk : $tanggal_masuk)"?><br>
            <?php
        }?>
        <strong>Gaji Bulan : </strong><?=$bln . " / " . $tahun?><br><br><br>

        <?php if ($error != "") { ?>
        <div class="BoxMessage Warning">Proses Gagal, <br /><?php echo $error; ?></div>
            <?php } ?>

        <div class="DivTable">
            <?=form_open("", "method='POST'")?>
            <table class="GridTable" style="width: 75%;">
                <tr class="GridHeader">
                    <td colspan="3">Tabel data gaji</td>
                </tr>
                <tr class="GridItem1">
                    <td>Gaji Pokok</td>
                    <td>&nbsp;</td>
                    <td>Rp. <?=form_input(array("name" => "gaji_pokok", "class" => "price subtotal", "id" => "gaji_pokok", "maxlength" => 11), $result->gaji_pokok)?></td>
                </tr>
                <tr class="GridItem1">
                    <td>Tunj. Jabatan</td>
                    <td>&nbsp;</td>
                    <td>Rp. <?=form_input(array("name" => "tunjangan_jabatan", "class" => "price subtotal tunjangan", "id" => "tunjangan_jabatan", "maxlength" => 11), $result->tunjangan_jabatan)?></td>
                </tr>

                <?php if($id_divisi != 8): ?>

                <tr class="GridItem1">
                    <td>Tunj. Anak-Istri</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr class="GridItem1">
                    <td>&nbsp;</td>
                    <td>Anak 1 (<?=form_input(array("name" => "prosentase_tunj_anak_1", "class" => "angka tunjangan_ai", "maxlength" => 3), $result->prosentase_tunj_anak_1)?>% x Gaji Pokok)</td>
                    <td>Rp. <span id="prosentase_tunj_anak_1" style="float: right;" class="subtotal">0</span></td>
                </tr>
                <tr class="GridItem1">
                    <td>&nbsp;</td>
                    <td>Anak 2 (<?=form_input(array("name" => "prosentase_tunj_anak_2", "class" => "angka tunjangan_ai", "maxlength" => 3), $result->prosentase_tunj_anak_2)?>% x Gaji Pokok)</td>
                    <td>Rp. <span id="prosentase_tunj_anak_2" style="float: right;" class="subtotal">0</span></td>
                </tr>
                <tr class="GridItem1">
                    <td>&nbsp;</td>
                    <td>Anak 3 (<?=form_input(array("name" => "prosentase_tunj_anak_3", "class" => "angka tunjangan_ai", "maxlength" => 3), $result->prosentase_tunj_anak_3)?>% x Gaji Pokok)</td>
                    <td>Rp. <span id="prosentase_tunj_anak_3" style="float: right;" class="subtotal">0</span></td>
                </tr>
                <tr class="GridItem1">
                    <td>&nbsp;</td>
                    <td>Istri (<?=form_input(array("name" => "prosentase_tunj_istri_1", "class" => "angka tunjangan_ai", "maxlength" => 3), $result->prosentase_tunj_istri_1)?>% x Gaji Pokok)</td>
                    <td>Rp. <span id="prosentase_tunj_istri_1" style="float: right;" class="subtotal">0</span></td>
                </tr>   

                <tr class="GridItem1">
                    <td>Tunj. Makan</td>
                    <td>
                        (
                        Rp. <?=form_input(array("name" => "tunjangan_makan", "class" => "price tunjangan_lain", "maxlength" => 11), $result->tunjangan_makan, "style='float: none;'")?>
                        x
                        <span id="jumlah_tunjangan_makan"><?=$jml_hari?></span>
                        )
                    </td>
                    <td>Rp. <span id="tunjangan_makan" style="float: right;" class="subtotal"></span></td>
                </tr>
                <tr class="GridItem1">
                    <td>Tunj. Transportasi</td>
                    <td>
                        (
                        Rp. <?=form_input(array("name" => "tunjangan_transportasi", "class" => "price tunjangan_lain", "maxlength" => 11), $result->tunjangan_transportasi, "style='float: none;'")?>
                        x
                        <span id="jumlah_tunjangan_transportasi"><?=$jml_hari?></span>
                        )
                    </td>
                    <td>Rp. <span id="tunjangan_transportasi" style="float: right;" class="subtotal"></span></td>
                </tr>
                <tr class="GridItem1">
                    <td>Tunj. Kehadiran</td>
                    <td>
                        (
                        Rp. <?=form_input(array("name" => "tunjangan_kehadiran", "class" => "price tunjangan_lain", "maxlength" => 11), $result->tunjangan_kehadiran, "style='float: none;'")?>
                        x
                        <span id="jumlah_tunjangan_kehadiran">0</span>
                        )
                    </td>
                    <td>Rp. <span id="tunjangan_kehadiran" style="float: right;" class="subtotal"></span></td>
                </tr>
                <tr class="GridItem1">
                    <td>Lembur</td>
                    <td>&nbsp;</td>
                    <td>
                        Rp. <?=form_input(array("name" => "total_lembur", "class" => "price subtotal tunjangan read-only", "id" => "total_lembur", "readonly" => "readonly"), $result->total_lembur)?>
                    </td>
                </tr>
                <tr class="GridItem1">
                    <td>Dinas Luar</td>
                    <td>
                        (
                        Rp. <?=form_input(array("name" => "tunjangan_dinas", "class" => "price tunjangan_lain", "maxlength" => 11), $result->tunjangan_dinas, "style='float: none;'")?>
                        x
                        <span id="jumlah_tunjangan_dinas">0</span>
                        )
                    </td>
                    <td>Rp. <span id="tunjangan_dinas" style="float: right;" class="subtotal"><?=$result->tunjangan_dinas*$jumlah_dinas;?></span></td>
                </tr>
                <tr class="GridItem1">
                    <td>Potongan Hutang</td>
                    <td>
                            <?php
                            if($id_hutang != ""):
                                ?>
                        <a href="javascript:void(0)" onclick="lihat_hutang('<?=$nik?>', '<?=$id_hutang?>')">Lihat Data Hutang</a>
                            <?php
                            endif;
                            ?>
                    </td>
                    <td>Rp. <?=form_input(array("name" => "potongan_hutang", "class" => "price subtotal tunjangan", "id" => "potongan_hutang"), $result->potongan_hutang)?></td>
                </tr>
                <?php else: ?>
                    <?=form_hidden("prosentase_tunj_anak_1", 0)?> 	<span id="prosentase_tunj_anak_1" style="display: none;">0</span>
                    <?=form_hidden("prosentase_tunj_anak_2", 0)?> 	<span id="prosentase_tunj_anak_2" style="display: none;">0</span>
                    <?=form_hidden("prosentase_tunj_anak_3", 0)?> 	<span id="prosentase_tunj_anak_3" style="display: none;">0</span>
                    <?=form_hidden("prosentase_tunj_istri_1", 0)?> 	<span id="prosentase_tunj_istri_1" style="display: none;">0</span>

                    <?=form_hidden("tunjangan_makan", 0)?>			 <span id="tunjangan_makan" style="display: none;">0</span>
                    <?=form_hidden("tunjangan_transportasi", 0)?>	 <span id="tunjangan_transportasi" style="display: none;">0</span>
                    <?=form_hidden("tunjangan_kehadiran", 0)?>		 <span id="tunjangan_kehadiran" style="display: none;">0</span>
                    <?=form_hidden("tunjangan_dinas", 0)?>			 <span id="tunjangan_dinas" style="display: none;">0</span>
                    <?=form_hidden("potongan_hutang", 0)?>			 <span id="potongan_hutang" style="display: none;">0</span>

                    <?=form_hidden("total_lembur", 0)?>
                <tr class="GridItem1">
                    <td>Detail Hutang</td>
                    <td>
                            <?php
                            if($id_hutang != ""):
                                ?>
                        <a href="javascript:void(0)" onclick="lihat_hutang('<?=$nik?>', '<?=$id_hutang?>')">Lihat Data Hutang</a>
                            <?php
                            else:
                                echo "-";
                            endif;
                            ?>
                    </td>
                    <td>&nbsp;</td>
                </tr>
                <?php endif; ?>

                <tr class="GridHeader">
                    <td align="right" colspan="2">Gaji Diterima</td>
                    <td>Rp. <?=form_input(array("name" => "gaji_diterima", "class" => "price read-only", "id" => "gaji_diterima", "readonly" => "readonly"), $result->gaji_diterima)?></td>
                </tr>
                <tr>
                    <td align="right" colspan="3">
                        <?=form_submit(array("name" => "btnSubmit"), "Simpan Data Gaji")?>
                        <input type="button" value="Cetak Slip Gaji" onclick="open_slip_gaji()">
                    </td>
                </tr>
            </table>
            <?=form_close()?>
        </div>
    </div>
</div>

<script language="javascript" type="text/javascript">
    jQuery("#gaji_pokok").keyup(gapok_update);
    jQuery("#gaji_pokok").blur(gapok_update);
    jQuery(".tunjangan_ai").keyup(update_tunjangan_anak_istri);
    jQuery(".tunjangan_ai").blur(update_tunjangan_anak_istri);
    jQuery(".tunjangan_lain").keyup(update_tunjangan_lain);
    jQuery(".tunjangan_lain").blur(update_tunjangan_lain);
    jQuery(".tunjangan").keyup(update_tunjangan_lain);
    jQuery(".tunjangan").blur(update_tunjangan_lain);

    get_parent_elm('jumlah_tunjangan_dinas', 'sum_dinas');
    get_parent_elm('jumlah_tunjangan_kehadiran', 'sum_normal', 'sum_dinas', 'sum_cuti');
    get_parent_elm('jumlah_tunjangan_transportasi', 'sum_normal', 'sum_dinas', 'sum_cuti');
    get_parent_elm('jumlah_tunjangan_makan', 'sum_normal', 'sum_dinas', 'sum_cuti');
    gapok_update();

    function open_slip_gaji(){
        var param=
            'gaji_pokok='+jQuery("#gaji_pokok").val()
            +'&jabatan='+jQuery("#tunjangan_jabatan").val()
            +'&anak_1='+jQuery("#prosentase_tunj_anak_1").html()
            +'&anak_2='+jQuery("#prosentase_tunj_anak_2").html()
            +'&anak_3='+jQuery("#prosentase_tunj_anak_3").html()
            +'&anak_4='+jQuery("#prosentase_tunj_anak_4").html()
            +'&istri_1='+jQuery("#prosentase_tunj_istri_1").html()
            +'&istri_2='+jQuery("#prosentase_tunj_istri_1").html()
            +'&makan='+jQuery("#tunjangan_makan").html()
            +'&transportasi='+jQuery("#tunjangan_transportasi").html()
            +'&kehadiran='+jQuery("#tunjangan_kehadiran").html()
            +'&lembur='+jQuery("#total_lembur").val()
            +'&dinas='+jQuery("#tunjangan_dinas").html()
            +'&hutang='+jQuery("#potongan_hutang").val()
            +'&gaji_total='+jQuery("#gaji_diterima").val()
            +'&nik=<?=$result->nik?>'
            +'&bulan=<?=$bulan?>'
            +'&tahun=<?=$tahun?>'
        ;
        window.open('<?php echo base_url(); ?>index.php/karyawan/print_gaji/?'+param, '_blank');
    }

    function get_parent_elm(elm, parent_elm, parent_elm2){
        var pre1 = parent.parent.get_elm_html(parent_elm);

        if(typeof parent_elm2 != 'undefined'){
            pre1 = parseInt(pre1) + parseInt(parent.parent.get_elm_html(parent_elm2));
        }

        jQuery("#"+elm).html(pre1);
    }


    function get_parent_elm(elm, parent_elm, parent_elm2, parent_elm3){
        var pre1 = parent.parent.get_elm_html(parent_elm);

        if(typeof parent_elm2 != 'undefined'){
            pre1 = parseInt(pre1) + parseInt(parent.parent.get_elm_html(parent_elm2));
        }

        if(typeof parent_elm3 != 'undefined'){
            pre1 = parseInt(pre1) + parseInt(parent.parent.get_elm_html(parent_elm3));
        }

        jQuery("#"+elm).html(pre1);
        update_tunjangan_lain();
    }

    function gapok_update(){                
        update_tunjangan_anak_istri();
        update_lembur();
    }

    function lihat_hutang(nik, id){
        return GB_showCenter('Data Hutang ' + nik, '<?php echo base_url(); ?>index.php/hutang/data_hutang_add/' + id, 600, 800);
    }

    function update_tunjangan_anak_istri(){
        var gapok = jQuery("#gaji_pokok").val();

        jQuery(".tunjangan_ai").each(function(){
            jQuery("#"+this.name).html(this.value * gapok / 100);
        });

        update_total();
    }

    function update_tunjangan_lain(){        
        jQuery(".tunjangan_lain").each(function(){
            var nilai = jQuery("#jumlah_"+this.name).html();

            jQuery("#"+this.name).html(this.value * parseInt(nilai));
        });

        update_total();
    }

    function update_lembur(){
        var gapok = jQuery("#gaji_pokok").val();
        var pros_lembur = parent.parent.get_pros_lembur();                
        jQuery("#total_lembur").val(parseFloat(pros_lembur) * gapok / 100);        
        update_total();
    }

    function update_total(){
        var total = 0;
        try{
            jQuery(".subtotal").each(function(){
                if (this.id == 'potongan_hutang'){
                    total -= parseInt(this.value);
                } else if(this.className.indexOf('tunjangan') != -1 || this.id == 'gaji_pokok'){
                    total += parseInt(this.value);
                } else {
                    total += parseInt(this.innerHTML);
                }
            });
        } catch(e){alert(e)}
        jQuery("#gaji_diterima").val(total);
    }
</script>